﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using LinqToTwitter;
using IGG.POCO;

namespace IGGCore
{
    /// <summary>
    /// ctouzel account
    /// </summary>
    public class TwitterParser
    {
        private TwitterContext twitterContext;

        public TwitterParser()
        {
            var authorizer = new SingleUserAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey = "h6YD9z2ZV8DA9MYAwvysdA",
                    ConsumerSecret = "Ul3ZINwTme7PpxsyGXrRWW3ThawmFdNfbHbiCG5CrMc",
                    OAuthToken = "222641430-zdVqDBVdAjDg6sEGWNuCMXavXfB2HvyZMmcxrIAH",
                    AccessToken = "D5ROydY59l3wEH1DWVhq0H7qrsiLGNJaoVBWeHYLDKxVd"
                }
            };
            twitterContext = new TwitterContext(authorizer);
        }

        public Tweet[] GetBeatTweets(List<string> writers, int num)
        {
            Tweet[] tweets = new Tweet[num];
            List<Tweet> tweetList = new List<Tweet>();

            foreach(string writer in writers)
            {
                var statusTweets = from tweet in twitterContext.Status
                                   where tweet.Type == StatusType.User &&
                                         tweet.ScreenName == writer &&
                                         tweet.IncludeContributorDetails == true &&
                                         tweet.Count == num * 10 &&
                                         tweet.IncludeEntities == true
                                   select tweet;
                int index = 0;
                foreach (var statusTweet in statusTweets)
                {
                    if (statusTweet.Text.StartsWith("@") == false)
                    {
                        Tweet tw = new Tweet();
                        tw.Author = statusTweet.User.Name;
                        tw.DateTime = statusTweet.CreatedAt;
                        tw.Text = ToolBox.FormatWebContent(statusTweet.Text);
                        tweetList.Add(tw);
                        index++;
                        if (index >= num)
                        {
                            break;
                        }
                    }
                }
            }
            var sorted = tweetList.OrderByDescending(x => x.DateTime);
            int idx = 0;
            foreach(Tweet tw in sorted)
            {
                tweets[idx] = tw;
                idx++;
                if (idx >= num)
                {
                    break;
                }
            }

            return tweets;
        }


        public Tweet[] GetTouzelTweets(int num)
        {
            Tweet[] tweets = new Tweet[num];

            var statusTweets = from tweet in twitterContext.Status
                               where   tweet.Type == StatusType.Home &&
                                       tweet.Count == num*10 &&
                                       tweet.IncludeEntities == true
                                select tweet;

            int index = 0;
            foreach (var statusTweet in statusTweets)
            {
                if (statusTweet.Text.StartsWith("@") == false)
                {
                    tweets[index] = new Tweet();
                    tweets[index].Author = statusTweet.User.Name;
                    tweets[index].DateTime = statusTweet.CreatedAt;
                    tweets[index].Text = ToolBox.FormatWebContent(statusTweet.Text);
                    index++;
                    if (index >= num)
                    {
                        break;
                    }
                }
            }

             return tweets;
        }

    }
}
